﻿using MagicWorld.DDD.Domain.Entity;
using MagicWorld.DDD.Domain.Repository;
using MagicWorld.DDD.Domain.Service.DamagePolicy;

namespace MagicWorld.DDD.Domain.Service.Impl
{
    public class CombatServiceImpl : ICombatService
    {
        private IWeaponRepository weaponRepository;
        private DamageManager damageManager;

        public CombatServiceImpl(IWeaponRepository weaponRepository, DamageManager damageManager)
        {
            this.weaponRepository = weaponRepository;
            this.damageManager = damageManager;
        }

        public void PerformAttack(Player player, Monster monster)
        {
            Weapon weapon = weaponRepository.Find(player.WeaponId);
            var damage = damageManager.CalculateDamage(player, weapon, monster);
            if (damage > 0)
            {
                monster.TakeDamage(damage);
                if (!monster.IsAlive())
                {
                    player.ReceiveExp(10); // 增加经验
                    if (player.CanLevelUp())
                    {
                        player.LevelUp(); // 升级
                    }
                }
            }
        }
    }
}
